<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\AssistWordExistsException;
use App\Exceptions\AssistWordNotFoundException;
use App\Exceptions\AssistWordUsedException;
use App\Models\AssistWord;

class AssistWordController extends Controller
{
    /*
     * @api get /course-api/a/assist-word 辅助词列表
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     * @param query string [name] 名称
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            'pageSize' => "integer|max:100|min:5",
            'page' => "integer|min:1",
            'name' => 'string'
        ]);

        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $name = $this->request->input('name');
        $query = AssistWord::query();

        if (isset($name)) {
            $query->where('en_name', 'like', "%{$name}%")
            ->orWhere('cn_name', 'like', "%{$name}%");
        }

        $total = $query->count();
        $assistWordsObj = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            /** @var AssistWord $assistWordObj */
            foreach ($assistWordsObj as $assistWordObj) {
                $items[] = [
                    'id' => $assistWordObj->getId(),
                    'name' => $assistWordObj->getName(),
                    'chinese' => $assistWordObj->getChinese(),
                    'partOfSpeech' => $assistWordObj->getPartOfSpeech(),
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }

    /*
     * @api post /course-api/a/assist-word 新增辅助词
     * @version 0.0.1
     * @group admin
     *
     * @param body string name 名称
     * @param body string chinese 中文
     * @param body int partOfSpeech 词性
     * @param body int audioId 音频
     * @param body int [coverId] 图片
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":"id"
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'name' => 'required|string',
            'chinese' => 'required|string',
            'partOfSpeech' => 'required|string',
            'audioId' => 'required|integer',
            'coverId' => 'integer',
        ]);
        $name = $this->request->input('name');
        $chinese = $this->request->input('chinese');
        $partOfSpeech = $this->request->input('partOfSpeech');
        $audioId = $this->request->input('audioId');
        $cover = $this->request->input('coverId', 0);

        if (AssistWord::where('en_name', $name)->where('part_of_speech', $partOfSpeech)->exists()) {
            throw new AssistWordExistsException();
        }
        $assistWordObj = new AssistWord();
        $assistWordObj->setName($name);
        $assistWordObj->setChinese($chinese);
        $assistWordObj->setPartOfSpeech($partOfSpeech);
        $assistWordObj->setAudio($audioId);
        $assistWordObj->setCover($cover);
        $assistWordObj->save();
        return $this->sendSuccessJson($assistWordObj->getId());
    }

    /*
     * @api put /course-api/a/assist-word/{wordId} 更新辅助词
     * @version 0.0.1
     * @group admin
     *
     * @param path int wordId 辅助词ID
     * @param body string name 名称
     * @param body string chinese 中文
     * @param body int partOfSpeech 词性
     * @param body int audioId 音频
     * @param body int [coverId] 图片
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function update($wordId)
    {
        $this->request->merge(['id'=>$wordId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'name' => 'required|string',
            'chinese' => 'required|string',
            'partOfSpeech' => 'required|string',
            'audioId' => 'required|integer',
            'coverId' => 'integer',
        ]);
        $name = $this->request->input('name');
        $chinese = $this->request->input('chinese');
        $partOfSpeech = $this->request->input('partOfSpeech');
        $audioId = $this->request->input('audioId');
        $cover = $this->request->input('coverId', 0);
        $existsId = AssistWord::where('en_name', $name)->where('part_of_speech', $partOfSpeech)->value('id');
        if ($existsId && $existsId != $wordId) {
            throw new AssistWordExistsException();
        }
        /** @var AssistWord $assistWordObj */
        $assistWordObj = AssistWord::find($wordId);
        if (!$assistWordObj) {
            throw new AssistWordNotFoundException();
        }
        $assistWordObj->setName($name);
        $assistWordObj->setChinese($chinese);
        $assistWordObj->setPartOfSpeech($partOfSpeech);
        $assistWordObj->setAudio($audioId);
        $assistWordObj->setCover($cover);
        $assistWordObj->save();
        return $this->sendSuccessJson($assistWordObj->getId());
    }

    /*
     * @api get /course-api/a/assist-word/{wordId} 辅助词详情
     * @version 0.0.1
     * @group admin
     *
     * @param path int wordId 辅助词ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function show($wordId)
    {
        $wordId = intval($wordId);
        /** @var AssistWord $assistWordObj */
        $assistWordObj = AssistWord::find($wordId);
        if (!$assistWordObj) {
            throw new AssistWordNotFoundException();
        }
        $item = [
            'id' => $assistWordObj->getId(),
            'name' => $assistWordObj->getName(),
            'chinese' => $assistWordObj->getChinese(),
            'partOfSpeech' => $assistWordObj->getPartOfSpeech(),
            'audio' => $assistWordObj->audio()->first(['id', 'name', 'uri', 'ext']),
            'cover' => $assistWordObj->cover()->first(['id', 'name', 'uri', 'ext']),
        ];
        return $this->sendSuccessJson($item);
    }

    /*
     * @api delete /course-api/a/assist-word/{wordId} 删除辅助词
     * @version 0.0.1
     * @group admin
     *
     * @param path int wordId 辅助词ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "items":[]
     * }
     * }
     */
    public function destroy($wordId)
    {
        $wordId = intval($wordId);
        /** @var AssistWord $assistWordObj */
        $assistWordObj = AssistWord::find($wordId);
        if (!$assistWordObj) {
            throw new AssistWordNotFoundException();
        }
        if ($assistWordObj->lesson()->exists()) {
            throw new AssistWordUsedException();
        }
        AssistWord::destroy($wordId);
        return $this->sendSuccessJson();
    }
}
